package com.pig4cloud.pigx.auth.handler;

import com.pig4cloud.pigx.common.security.constant.CacheConstants;
import com.pig4cloud.pigx.common.security.handler.AuthenticationSuccessHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/8/5 10:02
 **/
@Slf4j
@Component
@AllArgsConstructor
public class PigxAuthenticationSuccessLockEventHandler implements AuthenticationSuccessHandler {

    private final RedisTemplate<String, String> redisTemplate;

    /**
     * 登录成功清除锁定功能
     * {@link PigxAuthenticationFailureLockEventHandler}
     *
     * @param authentication 认证信息
     * @param request        请求信息
     * @param response       响应信息
     */
    @Override
    public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        String username = authentication.getName();
        String key = String.format("%s:%s", CacheConstants.LOGIN_ERROR_TIMES, username);
        redisTemplate.delete(key);
    }
}
